//
//  SetViewController.m
//  iStore
//
//  Created by Geek on 15/4/22.
//  Copyright (c) 2015年 mirrtalk. All rights reserved.
//

#import "SetViewController.h"
#import "SetTableViewCell.h"
#import "MessageViewController.h"
#import "InformationViewController.h"
#import "FeedbackViewController.h"
#import "AboutViewController.h"
#import "NewLogViewController.h"
#import "ChangePasswordViewController.h"
#import "InviteViewController.h"

@interface SetViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic) UIAlertView *alert;
@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"设置";
    
}
#pragma mark -- tableView 协议
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellId = @"cellId";
    SetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        cell =  [[[NSBundle mainBundle]loadNibNamed:@"SetTableViewCell" owner:self options:nil]lastObject];
    }
    
    NSArray *titleArray = @[@"消息设置",@"个人资料",@"我要推荐",@"更改密码",@"问题反馈",@"关于我们"];
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.text = titleArray[indexPath.section];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

// 点击进入下一个页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)  {
        MessageViewController *mVC = [[MessageViewController alloc]init];
        [self.navigationController pushViewController:mVC animated:YES];
    }
    
    else if (indexPath.section == 1){
        InformationViewController *iVC = [[InformationViewController alloc]init];
        [self.navigationController pushViewController:iVC animated:YES];
    }
    else if (indexPath.section == 2){
        InviteViewController *invc = [[InviteViewController alloc]init];
        [self.navigationController pushViewController:invc animated:YES];
    }
    else if (indexPath.section == 3){
            NSString *platformType = [[NSUserDefaults standardUserDefaults]objectForKey:KplatformType];
            if ([[NSString stringWithFormat:@"%@",platformType] isEqualToString:@"2"]) {
                alert(@"不能修改第三方账号密码");
            }
            else{
                ChangePasswordViewController *cvc = [[ChangePasswordViewController alloc]init];
                [self.navigationController pushViewController:cvc animated:YES];
            }

    }
    else if (indexPath.section == 4){
        FeedbackViewController * fVC = [[FeedbackViewController alloc]init];
        [self.navigationController pushViewController:fVC animated:YES];
     
    }else if (indexPath.section == 5){
        AboutViewController *aVC = [[AboutViewController alloc]init];
        [self.navigationController pushViewController:aVC animated:YES];
    
    }
    
}

- (IBAction)backButtonClick:(UIButton *)sender {
    if (_alert == nil) {
        _alert = [[UIAlertView alloc] initWithTitle:@"确定退出吗？" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    }
    [_alert show];
    
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
        NewLogViewController *loginVc = [[NewLogViewController alloc] init];
        UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:loginVc];
        [self presentViewController:navigation animated:YES completion:nil];
    }
}

@end
